'use strict';

/**
 * @ngdoc service
 * @name webApp.popBox
 * @description
 * # popBox
 * Service in the webApp.
 */
//封装移动端弹出框
var myPopup; //弹出框对象
angular.module('webApp')
  .factory('popBox',function($ionicPopup,$location,promiseFooter,jqLite,$localStorage){
  var showPopup = function($scope,title,subTitle,replice) {
    myPopup = $ionicPopup.show({
      template: '<div ng-controller="popImg">' +
      '<div class="changeImg" ng-click="changeImg()">' +
      '<img data="{{token}}" class="imagesGet" ng-src="{{imagesGet}}">' +
      '</div>' +
      '<input class="checkoutText" type="text">' +
      '</div>',
      title: title,
      subTitle: subTitle,
      scope: $scope,
      buttons: [
        { text: '取消' },
        {
          text: '<b>提交</b>',
          type: 'button-positive',
          onTap: function(e) {
            if(jqLite.$('checkoutText')[0].value != ''){
              if($scope.commitTitle == '写评论'){
                promiseFooter.textMainComments(
                  jqLite.$('commentText')[0].value,
                  jqLite.$('checkoutText')[0].value,
                  jqLite.$('imagesGet')[0].attributes[0].value,
                  $localStorage.flag
                ).then(function(data){
                  showConfirm('提示','评论成功！');
                  $scope.$emit($location.$$path,{data: data,action: '写评论'});
                  $scope.$emit('footShow',{title: '',action: 'hide'});
                });
              }
              else if($scope.commitTitle == '写回复'){
                promiseFooter.replay(
                  jqLite.$('commentText')[0].value,
                  jqLite.$('checkoutText')[0].value,
                  jqLite.$('imagesGet')[0].attributes[0].value,
                  $localStorage.flag
                ).then(function(data){
                  showConfirm('提示','回复成功！');
                  $scope.$emit($location.$$path,{data: data,action: '写回复'});
                  $scope.$emit('footShow',{title: '',action: 'hide'});
                });
              }
            }
            else{
              showConfirm('警告','警告验证码不得为空！');
            }
          }
        }
      ]
    });
  };
  var choicePopup = function(scope,title) {
    myPopup = $ionicPopup.show({
      template: '<div ng-controller="radio">' +
      '<ion-radio ng-model="choice" value="man" ng-change="serverSideChange(1)">男</ion-radio>' +
      '<ion-radio ng-model="choice" value="woman" ng-change="serverSideChange(0)">女</ion-radio>' +
      '</div>',
      title: title,
      subTitle: '',
      buttons: [
        { text: '关闭' ,type: 'button-positive'}
      ]
    });
  };
  var showConfirm = function(title,data) {
    myPopup = $ionicPopup.confirm({
      title: title,
      template: data,
      subTitle: '',
      buttons: [
        { text: '关闭' ,type: 'button-positive'}
      ]
    });
  };
  var showAlert = function() {
    myPopup = $ionicPopup.alert({
      title: '天气预报',
      template: '<iframe width="130" scrolling="no" height="120" frameborder="0" allowtransparency="true" src="http://i.tianqi.com/index.php?c=code&id=4&icon=5&wind=1&num=6"></iframe>',
      buttons: [
        { text: '确定' ,type: 'button-positive'}
      ]
    });
  };
  return{
    // 输入框
    showPopup: function(scope,title,subTitle,img){
      if(myPopup){
        myPopup.close();
      }
      showPopup(scope,title,subTitle,img);
    },
    // 选择框
    choicePopup: function(scope,title){
      if(myPopup){
        myPopup.close();
      }
      choicePopup(scope,title);
    },
    // 提示框
    showConfirm: function(title,data){
      if(myPopup){
        myPopup.close();
      }
      showConfirm(title,data);
    },
    // 弹出框
    showAlert: function(){
      if(myPopup){
        myPopup.close();
      }
      showAlert();
    }
  }
});
